package com.jdk.learn.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * synchronized 默认锁的是当前对象，在同一个线程中当前对象是可重入的，即结果是交替执行
 * 等价于ReentrantLock
 */
public class LockSynchronizeDemo  implements Runnable{
    @Override
    public void run() {
        get();
    }

    public synchronized void get(){
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("name:"+Thread.currentThread().getName()+",get");
        set();
    }
    public synchronized void set(){
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("name:"+Thread.currentThread().getName()+",set");
    }

    public static void main(String[] args) {
        LockSynchronizeDemo lockSynchronizeDemo=new LockSynchronizeDemo();
        new Thread(lockSynchronizeDemo,"1").start();
        new Thread(lockSynchronizeDemo,"2").start();
        new Thread(lockSynchronizeDemo,"3").start();
        new Thread(lockSynchronizeDemo,"4").start();
    }
}
